package com.wenhao.leetcode.easy;

/**
 * 题目：https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
 * 买卖股票的最佳时机
 *
 * @author Wenhao Tong
 * @Description
 * @create 2021-07-01 13:35
 */
public class LeetCode122 {

    public static void main(String[] args) {

    }

    /**
     * 这是只可以买卖一次股票的最大收益
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        int[] result = new int[prices.length];
        int min = prices[0];
        for (int i = 1;i < result.length;i++) {
            min = min < prices[i] ? min : prices[i];
            int temp = prices[i] - min;
            result[i] = temp > result[i - 1] ? temp : result[i - 1];
        }
        return result[result.length - 1];
    }

    /**
     * 不限次数
     * @param prices
     * @return
     */
    public int maxProfit2(int[] prices) {
        int[] profit = new int[prices.length];
        profit[0] = 0;
        for (int i = 1;i < profit.length;i++) {
            if (prices[i] > prices[i - 1]) {
                profit[i] = profit[i - 1] + prices[i] - prices[i - 1];
            } else {
                profit[i] = profit[i - 1];
            }
        }
        return profit[profit.length - 1];
    }

}
